package designpattern.prototype.demo01;

import java.util.Date;

/**
 * @author cyt
 * @create 2020-04-23 16:20
 */

public class Test {
    /**
     * 原型模式。 就是能够对之前的对象进行克隆， 然后产生一个新的对象
     * 浅克隆
     * 1、实现 cloneenble 接口
     * 2、重写 clone方法
     *
     * @param args
     */
    public static void main(String[] args) throws CloneNotSupportedException {
        test();
    }

    @SuppressWarnings("all")
    static void test() throws CloneNotSupportedException {
        // 1、新建原型对象
        Date date = new Date();
        user u1 = new user(1, "cyt", date);
        user u2 = (user) u1.clone();

        System.out.println("u1->" + u1);
        System.out.println("u1.hashcode->" + u1.hashCode());
        System.out.println("u2->" + u2);
        System.out.println("u2.hashcode->" + u2.hashCode());
        System.out.println("========================");
        date.setTime(132456);
        System.out.println("u1->" + u1);
        System.out.println("u2->" + u2);
        /**
         * 重新设置原型的属性， clone 对象的值也会改变
         *u1->user{id=1, name='cyt', date=Thu Apr 23 16:41:19 CST 2020}
         * u1.hashcode->1807837413
         * u2->user{id=1, name='cyt', date=Thu Apr 23 16:41:19 CST 2020}
         * u2.hashcode->1254526270
         * ========================
         * u1->user{id=1, name='cyt', date=Thu Jan 01 08:02:12 CST 1970}
         * u2->user{id=1, name='cyt', date=Thu Jan 01 08:02:12 CST 1970}
         */
    }
}
